package org.example.demo_xiaowuz.controller;

import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.example.demo_xiaowuz.enums.StatusEnum;
import org.example.demo_xiaowuz.model.*;
import org.example.demo_xiaowuz.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    BookService bookService;
    @RequestMapping("getListByPage")
    public Result<PageResult<BookInfo>> getListByPage(RequestPage requestPage
            , HttpSession session) {
        PageResult<BookInfo> pageResult = bookService.getListByPage(requestPage);
        return Result.success(pageResult);
    }
    @RequestMapping("/addBook")
    public Boolean addBook(BookInfo bookInfo){
        if(!StringUtils.hasLength(bookInfo.getBookName())||
                !StringUtils.hasLength(bookInfo.getAuthor())||
                bookInfo.getCount()==null||bookInfo.getPrice()==null||
                !StringUtils.hasLength(bookInfo.getPublish())) {
            return false;
        }
        Integer ret=bookService.addBook(bookInfo);
        if(ret==0) return false;
        return true;
    }

    /**
     * 根据id获取图书信息
     * @param bookId
     * @return
     */
    @RequestMapping("getBookInfo")
    public BookInfo getBookInfo(Integer bookId) {
        BookInfo bookInfo=bookService.getBookInfo(bookId);
        if(bookInfo==null) {
            return null;
        }
        return bookInfo;
   }

    /**
     * 更新图书
     * @param bookInfo
     * @return
     */
    @RequestMapping("updateBook")
    public Boolean updateBook(BookInfo bookInfo) {
        log.info("updateBook, bookInfo: {}", bookInfo);
        if (bookInfo.getId()==null){
            log.warn("Id不能为空");
            return false;
        }
        if (bookInfo.getBookName()!=null && "".equals(bookInfo.getBookName().trim())){
            log.warn("图书名称不能为空");
            return false;
        }
        try {
            Integer ret=bookService.updateBook(bookInfo);
            if(ret==0) {
                return false;
            }
            return true;
        }catch (Exception e) {
            log.error("update error,e: {}",e);
            return false;
        }
    }

    /**
     * 根据id删除图书
     * @param bookId
     * @return
     */
    @RequestMapping("deleteBook")
    public Boolean deleteBook(Integer bookId) {
        log.info("deleteBook, id: "+bookId);
        try {
            BookInfo bookInfo = new BookInfo();
            bookInfo.setId(bookId);
            bookInfo.setStatus(StatusEnum.DELETED.getCode());
            Integer result = bookService.updateBook(bookInfo);
            if (result >0){
                return true;
            }
            return false;
        }catch (Exception e){
            log.error("deleteBook error, e: {}", e);
            return false;
        }
    }
    @RequestMapping("/batchDelete")
    public Boolean batchDelete(@RequestParam List<Integer> ids) {
        log.info("batchDelete ids:{}", ids);
        if (ids==null || ids.size()==0){
            return true;
        }
        try {
            Integer ret = bookService.batchDelete(ids);
            if(ret>0) {
                return true;
            }
            return false;
        }catch (Exception e) {
            log.error("batchDelete error: ", e);
            return false;
        }
    }
}
